﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Windows.Forms;
using System.IO;

namespace Expressing_Label
{
    public partial class PublicResources
    {
        public static string g_strConnectionCommon = "data source=219.224.20.69;initial catalog=db_FACE;user id=bnuface;password=wlzx@2013;";
        public static List<string> g_strConnection = new List<string>();
        public static Dictionary<string, string> g_dicConnection = new Dictionary<string, string>();
        public static string g_strCurrentConnection = "";
        public static List<string> g_ExpressingSourceItem = new List<string>();
        public static List<string> g_NameItem = new List<string>();
        public static List<string> g_DepartmentItem = new List<string>();
        public static List<string> g_VideoModelItem = new List<string>();
        public static List<string> g_VideoModelDetail = new List<string>();
        public static int g_iUserNum = 1;
        public static int g_iUserID = 0;
        public static string g_strSelfDiscribing = "自我描述";
        public static string g_strPhysicalCondition = "身体状况";
        public static DateTime g_dtStartTime = new DateTime();
        public static int g_ExperimentDuration = 40;
        public static string g_strWeather = "";
        public static string g_strLocation = "";
        public static string g_strUserCharacter = "";
        public static bool g_blUserGender = true;
        public static string g_strUserDepartment = "";
        public static string g_strImageBaseName = @"Capture\";
        public static string g_strImageLocation = "";
        public static int g_iConfiguraState = 1;
        public static Dictionary<string, int> g_ExpressingSelectedIndex = new Dictionary<string, int>();
        public static int[,] g_ExpresingCaptureRecord = new int[15, 25];
        public static int g_CaptureTimes = 0;
        public static int g_CaptureInTenSec = 0;
        public static string g_password =  "123456";
        public static int g_checkcount = 0;
        public static double g_dCurrentPosition = 0;
        public static string g_strVideoEffectivity = string.Empty;
        public static string g_strCharacter = string.Empty;
        public static string g_strFeeling = string.Empty;
        public static string g_strCharacterResult = string.Empty;
        public static string g_strFeelingResult = string.Empty;
        public static int g_iExperimentID = 0;
        public static int g_iNumToLabel = 200;

        public PublicResources()
        {
            //Load data from Application Start Location
            InitializeResourse();
            TempCheck();
        }

        public void InitializeResourse()
        {
            //Initialize the UserID
            string strBaseLocation = "Data\\";

            //Initialize the ExpressingSourceItem
            DataReader(strBaseLocation + "ExpressingSourceItem.data", g_ExpressingSourceItem);

            //Initialize the VideoModelItem and VideoDetailItem
            DataReader(strBaseLocation + "VideoModelItem.data", g_VideoModelItem);

            //Initialize the ConnectionString
            DataReader(strBaseLocation + "ConnectionString.data", g_strConnection);

            //Initialize the ConnectionDictinary
            g_dicConnection.Add("网络中心", System.Configuration.ConfigurationManager.ConnectionStrings["db_FACEEntities"].ToString());

            //Initialize the ExperimentStartTime
            g_dtStartTime = DateTime.Now;

            //Initialize the DepartmentItem
            DataReader(strBaseLocation + "DepartmentItem.data", g_DepartmentItem);

            //Initialize the NameItem
            DataReader(strBaseLocation + "NameItem.data", g_NameItem);

            //Initialize the Directory
            if (!Directory.Exists(@"Capture"))
                Directory.CreateDirectory(@"Capture");
            if (!Directory.Exists(@"Temp"))
                Directory.CreateDirectory(@"Temp");

            //Data Test
            //string s;
            //StreamReader sr = new StreamReader(strBaseLocation + "Test.data", Encoding.UTF8);
            //for (int i = 0; ; i++)
            //{
            //    if ((s = sr.ReadLine()) == null)
            //        break;
            //    string[] ts = System.Text.RegularExpressions.Regex.Split(s, @"[,]");
            //    for (int j = 0; j < ts.Count<string>(); j++)
            //    {
            //        g_ExpresingCaptureRecord[i, j] = Convert.ToInt32(ts[j]);
            //    }
            //}

        }

        private void TempCheck()
        {
            if(File.Exists(@"Data\r.tmp"))
            {
                using(StreamReader sr = new StreamReader(@"Data\r.tmp",Encoding.UTF8))
                {
                    string ts = sr.ReadLine();
                    if(ts == "False")
                    {
                        PublicResources.g_iConfiguraState = Convert.ToInt32(sr.ReadLine());
                        PublicResources.g_iUserNum = Convert.ToInt32(sr.ReadLine());
                        PublicResources.g_iUserID = Convert.ToInt32(sr.ReadLine());
                        PublicResources.g_dtStartTime = DateTime.Parse(sr.ReadLine());
                        PublicResources.g_strWeather = sr.ReadLine();
                        PublicResources.g_strLocation = sr.ReadLine();
                        PublicResources.g_blUserGender = Convert.ToBoolean(sr.ReadLine());
                        PublicResources.g_strUserCharacter = sr.ReadLine();
                        PublicResources.g_strUserDepartment = sr.ReadLine();
                        PublicResources.g_strPhysicalCondition = sr.ReadLine();
                        PublicResources.g_strSelfDiscribing = sr.ReadLine();
                        PublicResources.g_strImageBaseName = sr.ReadLine();
                        PublicResources.g_dCurrentPosition = Convert.ToDouble(sr.ReadLine());
                        PublicResources.g_CaptureTimes = Convert.ToInt32(sr.ReadLine());
                        PublicResources.g_iExperimentID = Convert.ToInt32(sr.ReadLine());
                        int c = Convert.ToInt32(sr.ReadLine());
                        PublicResources.g_ExpressingSourceItem.Clear();
                        for (int i = 0; i < c; i++)
                        {
                            PublicResources.g_ExpressingSourceItem.Add(sr.ReadLine());
                        }
                        c = Convert.ToInt32(sr.ReadLine());
                        for (int i = 0; i < c; i++)
                        {
                            PublicResources.g_VideoModelDetail.Add(sr.ReadLine());
                        }
                        for (int i = 0; i < 15; i++)
                        {
                            ts = sr.ReadLine();
                            string[] tts = System.Text.RegularExpressions.Regex.Split(ts, @"[ ]");
                            for (int j = 0; j < 25; j++)
                            {
                                PublicResources.g_ExpresingCaptureRecord[i, j] = Convert.ToInt32(tts[j]);
                            }
                        }
                        g_ExpressingSelectedIndex.Clear();
                        for (int i = 0; i < g_ExpressingSourceItem.Count; i++)
                        {
                            if (i % 5 > 0)
                            {
                                ts = g_ExpressingSourceItem[i];
                                int t = Convert.ToInt32(ts.Substring(ts.LastIndexOf('#') + 1));
                                ts = ts.Substring(0, ts.LastIndexOf('#'));
                                if (!ts.EndsWith("0"))
                                    g_ExpressingSelectedIndex.Add(ts, t++);
                            }
                        }
                        if (PublicResources.g_dCurrentPosition > 0)
                        {
                            QuestinnairRead();
                            PublicResources.g_iConfiguraState = 6;
                            
                        }
                    }
                }
            }
        }

        public void QuestinnairRead()
        {
            string str_Start = Application.StartupPath;
            str_Start = str_Start.Substring(0, str_Start.LastIndexOf("\\"));
            str_Start += "\\Debug\\Saved.data";
            if(File.Exists(str_Start))
            {
                using (StreamReader sr = new StreamReader(str_Start, Encoding.UTF8))
                {
                    sr.ReadLine();
                    sr.ReadLine();
                    for (int i = 0; i < 10; i++)
                    {
                        g_strVideoEffectivity += sr.ReadLine();
                        if (i < 9)
                            g_strVideoEffectivity += ",";
                    }
                    g_strCharacter = sr.ReadLine();
                    g_strFeeling = sr.ReadLine();
                    g_strCharacterResult = sr.ReadLine();
                    g_strFeelingResult = sr.ReadLine();
                }
            }
        }

        private void DataReader(string src, List<string> des)
        {
            var file = new FileInfo(@src);
            if (!file.Exists)
            {
                if (MessageBox.Show("未找到 " + src + ". 请联系秋月大人..") == DialogResult.OK)
                    System.Environment.Exit(0);
            }

            using (StreamReader sr = new StreamReader(src, Encoding.UTF8))
            {
                string s;
                while ((s = sr.ReadLine()) != null)
                    if (s != "")
                        des.Add(s);
            }
        }

    }
}
